package com.vitaf.cash.app.ui.fragment

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import com.vitaf.cash.app.R
import com.vitaf.cash.app.common.VitafComFmtV2
import com.vitaf.cash.app.configs.VitafConfigs
import com.vitaf.cash.app.databinding.DataBindingHelper
import com.vitaf.cash.app.databinding.FragmentUserBinding
import com.vitaf.cash.app.event.VitafEventData
import com.vitaf.cash.app.ui.activity.VitafPrivacyPolicyAct
import com.vitaf.cash.app.ui.activity.VitafUserAdviceTypeAct
import com.vitaf.cash.app.ui.activity.VitafUserLoginAct
import com.vitaf.cash.app.ui.activity.SettingActivity
import com.vitaf.cash.app.utils.helper.VitafConfigsHp
import com.vitaf.cash.app.utils.helper.VitafCustomDataHp
import com.vitaf.cash.app.utils.shared.VitafSharedHp
import com.vitaf.cash.app.utils.shared.VitafSharedKey

/**
 * VitafCash
 * @Author wwdeng
 * @CreateTime: 2024/12/10 14:53
 * @Annotation：我的
 */
class TabUserFragment(): VitafComFmtV2() {
    private var _binding: FragmentUserBinding? = null

    override val pageName: String = "User_Page"

    private var viewModel = DataBindingHelper.createModel()

    override fun createVitafContentView(inflater: LayoutInflater): View? {
        _binding = FragmentUserBinding.inflate(layoutInflater)
        return _binding?.root
    }

    override fun initializeVitafViews() {
        val binding = _binding ?: return
        binding.userFragment = this
        binding.model = viewModel
        binding.lifecycleOwner = this

        setViewPaddingTopStatusBarHeight(binding.userRefreshLayout)
        initUserCenterRefreshView()
    }

    override fun setupVitafEventListeners() {
    }

    @SuppressLint("SetTextI18n")
    override fun loadVitafData() {
        //是否有新版本
        val isUpdate = VitafConfigsHp.isGombeCreditUpdate()
        if (isUpdate){
            _binding?.userSettingPoint?.visibility = View.VISIBLE
        } else {
            _binding?.userSettingPoint?.visibility = View.GONE
        }
        //加载保存自定义数据
        VitafCustomDataHp.takeGombeCreditCustomDataAction()
    }

    /* 初始化刷新 */
    private fun initUserCenterRefreshView() {
        _binding?.userRefreshLayout?.setColorSchemeResources(R.color.theme,R.color.purple_500)
        _binding?.userRefreshLayout?.setOnRefreshListener {
            VitafCustomDataHp.takeGombeCreditCustomDataAction()
        }
    }

    /**
     * 隐私协议
     */
    fun onUserClickPrivacyPolicy(){
        addEventPointRequestAction("${pageName}_Privacy_Policy_Click".uppercase())
        val showTitle = getString(R.string.privacy_policy_title)
        val linkUrl = VitafConfigs.GOMBECREDIT_PRIVACY_POLICY_SENEGAL_LINK
        VitafPrivacyPolicyAct.start(requireContext(), showTitle, linkUrl)
    }

    /**
     * 意见反馈
     */
    fun onUserClickFeedback(){
        addEventPointRequestAction("${pageName}_Feedback_Click".uppercase())
        onIntent(VitafUserAdviceTypeAct::class.java)
    }

    /**
     * 设置
     */
    fun onUserClickSetting(){
        addEventPointRequestAction("${pageName}_Setting_Click".uppercase())
        onIntent(SettingActivity::class.java)
    }

    /**
     * 退出
     */
    fun onUserClickExit(){
        addEventPointRequestAction("${pageName}_Exit_Click".uppercase())
        //退出登录，需要移除用户保存的信息
        VitafSharedHp.onGombeCreditLoginOut()
        Handler(Looper.getMainLooper()).postDelayed({
            onIntent(VitafUserLoginAct::class.java)
        },300)
    }

    override fun onVitafPublishEvent(event: VitafEventData) {
        super.onVitafPublishEvent(event)
        if (event.eventType == VitafEventData.TYPE.GOMBECREDIT_CUSTOM_DATA_REFRESHED){
            hideVitafLoading()
            viewModel.set("mobile","+${VitafSharedHp.takeGombeCreditSharedString(VitafSharedKey.GOMBECREDIT_TIMEZONE_CODE)} ${VitafSharedHp.takeGombeCreditSharedString(VitafSharedKey.GOMBECREDIT_MOBILE)}")
        }
    }

    override fun hideVitafLoading() {
        super.hideVitafLoading()
        _binding?.userRefreshLayout?.isRefreshing = false
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}